package com.think.service;

import com.think.dao.mapper.PermissionDaoMapper;
import com.think.dao.mapper.UserDaoMapper;
import com.think.model.Permission;
import com.think.model.Role;
import com.think.model.User;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by yangyibo on 17/1/18.
 */
@Service
public class CustomUserService implements UserDetailsService { //自定义UserDetailsService 接口

    @Autowired
    UserDaoMapper userDaoMapper;
    @Autowired
    PermissionDaoMapper permissionDaoMapper;
    @Autowired
    HttpServletRequest request;
    private static final Logger logger = LoggerFactory.getLogger(CustomUserService.class);

    @Override
    public UserDetails loadUserByUsername(String username) { //重写loadUserByUsername 方法获得 userdetails 类型用户

        User user = userDaoMapper.findByUserName(username);
        if(user == null){
            throw new UsernameNotFoundException("用户名不存在");
        }
        List<SimpleGrantedAuthority> authorities = new ArrayList<>();
        //用于添加用户的权限。只要把用户权限添加到authorities 就万事大吉。
        for(Role role:user.getRoles())
        {
            authorities.add(new SimpleGrantedAuthority(role.getName()));
        }
        user.setGrantedAuthorities(authorities); //用于登录时 @AuthenticationPrincipal 标签取值
        return user;
    }
//    @Override
//    public UserDetails loadUserByUsername(String username) {
//        User user = userDaoMapper.findByUserName(username);
//        if (user != null) {
//            List<Permission> permissions = permissionDaoMapper.findByAdminUserId(user.getId());
//            List<GrantedAuthority> grantedAuthorities = new ArrayList <>();
//            HttpSession session = request.getSession();
//            session.setAttribute("user",user);
//            session.setAttribute("sessusername",username);
//            for (Permission permission : permissions) {
//                if (permission != null && permission.getName()!=null) {
//
//                    GrantedAuthority grantedAuthority = new SimpleGrantedAuthority(permission.getName());
//                    //1：此处将权限信息添加到 GrantedAuthority 对象中，在后面进行全权限验证时会使用GrantedAuthority 对象。
//                    grantedAuthorities.add(grantedAuthority);
//                }
//            }
//            return new org.springframework.security.core.userdetails.User(user.getUsername(), user.getPassword(), grantedAuthorities);
//        } else {
//            logger.info("admin: " + username + " do not exist!");
//            throw new UsernameNotFoundException("admin: " + username + " do not exist!");
//        }
//    }
}

